/*
 * Copyright (c) United Binary LLC.  All rights reserved.
 * 
 * This code is licensed under the MIT License
 * 
 * SEE: http://harnessit.codeplex.com/license
 * 
 */
using System;
using System.Collections;
using System.Diagnostics;

using UnitedBinary.HarnessIt.TestEngine.PortableTestItems;

namespace UnitedBinary.HarnessIt.TestEngine
{
	/// <summary>
	/// Summary description for TestTraceListener.
	/// </summary>
	public class TestTraceListener : TraceListener
	{
		private ArrayList traceMessageList = new ArrayList();
		
		public TestTraceListener()
		{
		}

		private void AddTraceMessageObject(string message)
		{
			traceMessageList.Add(message);
		}

		public string [] TraceMessages
		{
			get
			{
				string [] messageArray = new string [traceMessageList.Count];
				traceMessageList.CopyTo(messageArray, 0);

				return messageArray;
			}
		}

		public override void Write(object o)
		{
			AddTraceMessageObject(o.ToString());
		}

		public override void Write(string message)
		{
			AddTraceMessageObject(message);
		}

		public override void Write(string message, string category)
		{
			AddTraceMessageObject(category + ": " + message);
		}

		public override void Write(object o, string category)
		{
			AddTraceMessageObject(category + ": " + o.ToString());
		}

		public override void WriteLine(object o)
		{
			AddTraceMessageObject(o.ToString() + "\n");
		}

		public override void WriteLine(string message)
		{
			AddTraceMessageObject(message + "\n");
		}

		public override void WriteLine(string message, string category)
		{
			AddTraceMessageObject(category + ": " + message + "\n");
		}

		public override void WriteLine(object o, string category)
		{
			AddTraceMessageObject(category + ": " + o.ToString() + "\n");
		}

		public void AppendTraceMessages(PortableExecutionResult per)
		{
			string [] messages = this.TraceMessages;

			if (messages.Length == 0)
			{
				return;
			}

			PortableTestObject pto = null;
			int messageLength = messages.Length;
			PortableTestObject [] PTOs = new PortableTestObject [messageLength];
			for (int i=0; i < messageLength; i++)
			{
				pto = new PortableTestObject();
				pto.Type = PortableTestObject.TestObjectType.Trace;
				pto.Comment = messages[i];

				PTOs[i] = pto;
			}
			
			per.AppendTestObjects(PTOs);	
		}
	}
}
